package com.euc.springbootframework.util;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.core.io.Resource;

/**
 * Created by WUNE on 5/7/2018.
 */
public class EmailUtils {

    public static void sendTextEmail(String from,String to,String cc,String subject,String content) throws Exception{
        JavaMailSender sender = SpringUtils.getBean(JavaMailSenderImpl.class);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setCc(cc);
        message.setSubject(subject);
        message.setText(content);
        sender.send(message);
    }

    public static void sendHtmlEmail(String from,String to,String cc,String subject,String content) throws Exception{
//        JavaMailSenderImpl mailSender2 = new JavaMailSenderImpl();
//        mailSender2.setHost("TESTAPPSMTP");
//        mailSender2.setPort(25);
//        mailSender2.setUsername("dicksam.feng@oocl.com");
//        mailSender2.setPassword("Password6");
//        mailSender2.setDefaultEncoding("UTF-8");
//        Properties prop = new Properties();
//        prop.put("mail.smtp.auth", "true");
//        mailSender2.setJavaMailProperties(prop);
        JavaMailSender sender = SpringUtils.getBean(JavaMailSenderImpl.class);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
        sender.send(message);
    }

    public static void sendAttachmentsEmail(String subject, String from, String to, String cc, String content, Map<String,File> attachmentFiles, Map<String,Resource> inLines) throws Exception{
        JavaMailSender sender = SpringUtils.getBean(JavaMailSenderImpl.class);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = null;
        if(inLines != null){
            helper = new MimeMessageHelper(message,true);
        }else{
            helper = new MimeMessageHelper(message);
        }
        helper.setFrom(from);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
        if(attachmentFiles != null && !attachmentFiles.isEmpty()){
            for (Iterator<Entry<String,File>> iterator = attachmentFiles.entrySet().iterator(); iterator.hasNext();) {
                Entry<String,File> entry = iterator.next();
                helper.addAttachment(entry.getKey(), entry.getValue());
            }
        }

        if(inLines != null && !inLines.isEmpty()){
            for (Iterator<Entry<String,Resource>> iterator = inLines.entrySet().iterator(); iterator.hasNext();) {
                Entry<String,Resource> entry = iterator.next();
                helper.addInline(entry.getKey(), entry.getValue());
            }
        }
        sender.send(message);
    }
}
